package com.sora.org.service.impl;

import com.sora.basic.service.impl.BaseServiceImpl;
import com.sora.basic.util.Md5Utils;
import com.sora.basic.util.StrUtils;
import com.sora.org.domain.Employee;
import com.sora.org.mapper.EmployeeMapper;
import com.sora.org.service.IEmployeeService;
import com.sora.user.domain.Logininfo;
import com.sora.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：部门
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        //生成32的盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //将密码通过盐值进行加密
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = new Logininfo();
        //利用属性复制工具进行属性复制
        BeanUtils.copyProperties(employee,logininfo);
        //将logininfo添加到数据库生成自增长id
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);

        //添加员工之后将自增长的id设置到t_employee_role表中
        if(employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee);
        }
    }

    @Override
    public void del(Long id) {
        //先查询employee对象从而得到logininfo_id
        Employee employee = employeeMapper.loadById(id);

        //先删除t_logininfo表中的对象
        logininfoMapper.del(employee.getLogininfo_id());

        //再删除t_employee_role表中的数据
        employeeMapper.delEmployeeRole(id);

        super.del(id);
    }

    @Override
    public void update(Employee employee) {
        //对修改后的密码进行加密加盐
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Pwd);

        //先从前端修改页面传递过来的值中的id找到最开始数据库中存放的employee对象
        Employee oldEmployee = employeeMapper.loadById(employee.getId());

        //从原来添加的employee对象中取出logininfo_id，shop_id设置到新的employee对象中
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());

        super.update(employee);

        //通过原来添加的员工对象的logininfo_id找到原来的logininfo对象
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        //将修改之后的employee对象中的属性复制到原来的登录信息中
        BeanUtils.copyProperties(employee,oldLogininfo);
        //前端传回的employee的logininfo_id是一个null值，所以重新设置oldLogininfo的logininfo_id
        oldLogininfo.setId(oldEmployee.getLogininfo_id());

        logininfoMapper.update(oldLogininfo);

        //同步t_employee_role表 -- 先删除，再添加
        employeeMapper.delEmployeeRole(employee.getId());
        if(employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee);
        }

    }


    @Override
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
